package com.itheima.web.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.Module;
import com.itheima.domain.Result;
import com.itheima.factory.BeanFactory;
import com.itheima.service.ModuleService;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/system/module/*")
public class ModuleServlet extends BaseServlet {
    private ModuleService moduleService = BeanFactory.getBean("moduleService",ModuleService.class);
    private Result findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            
            List<Module> list = moduleService.findAll();
            return new Result(true,MessageConstant.QUERY_MODULE_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MODULE_FAIL);
        }
    }

    private Result delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            String[] ids = BeanUtil.fillBeanFromJson(request, String[].class);
            
            moduleService.delete(ids);
            return new Result(true,MessageConstant.DELETE_COMPANY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_COMPANY_FAIL);
        }
    }

    private Result edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            Module module = BeanUtil.fillBeanFromJson(request, Module.class);
            
            moduleService.update(module);
            return new Result(true,MessageConstant.EDIT_COMPANY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_COMPANY_FAIL);
        }
    }
    private Result findModulesByRoleId(HttpServletRequest request, HttpServletResponse response) throws IOException {

        try {
            String roleId = request.getParameter("roleId");

            List<Map<String,Object>> list= moduleService.findModulesByRoleId(roleId);
            return new Result(true,MessageConstant.QUERY_MODULE_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MODULE_FAIL);
        }
    }
    private Result findById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            String id = request.getParameter("id");
            
            Module module = moduleService.findById(id);
            return new Result(true,MessageConstant.QUERY_COMPANY_SUCCESS,module);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_COMPANY_FAIL);
        }
    }

    private Result save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            Module module = BeanUtil.fillBeanFromJson(request, Module.class);
            
            moduleService.save(module);
            return new Result(true,MessageConstant.SAVE_COMPANY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SAVE_COMPANY_FAIL);
        }
    }

    private Result findByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            String currentPageStr = request.getParameter("currentPage");
            String pageSizeStr = request.getParameter("pageSize");
            int currentPage=1,pageSize=5;
            if (!StringUtils.isBlank(currentPageStr)){
                currentPage=Integer.parseInt(currentPageStr);
            }
            if (!StringUtils.isBlank(pageSizeStr)){
                pageSize=Integer.parseInt(pageSizeStr);
            }
            
            PageInfo<Module> info =moduleService.findByPage(currentPage,pageSize);
            return new Result(true, MessageConstant.QUERY_COMPANY_SUCCESS,info);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_COMPANY_FAIL);
        }

    }

    
}
